package com.wgts.wgts.mvp.presenter.imp;

import com.wgts.wgts.mvp.model.IMyExtensionFansFragmentModel;
import com.wgts.wgts.mvp.presenter.IMyExtensionFansFragmentpersenter;
import com.wgts.wgts.mvp.view.IMyExtensionFansFragmentView;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.req.FansSumReq;
import com.wgts.wgts.netmodle.req.ShopKeepFansReq;
import com.wgts.wgts.netmodle.resp.FansSumEntity;
import com.wgts.wgts.netmodle.resp.ShopKeepFansEntity;

public class MyExtensionFansFragmentpersenter implements IMyExtensionFansFragmentpersenter {

    private IMyExtensionFansFragmentView view;

    private IMyExtensionFansFragmentModel model;

    private int pageIndex = 1;

    private int pageSize = 20;

    private int level;

    private int userId;

    private ShopKeepFansEntity entity;


    public MyExtensionFansFragmentpersenter(IMyExtensionFansFragmentView view, IMyExtensionFansFragmentModel model) {
        this.view = view;
        this.model = model;
    }

    @Override
    public void initFans(int leve, int userId) {
        ShopKeepFansReq req = new ShopKeepFansReq();

        pageIndex = 1;
        this.level = leve;
        this.userId = userId;
        req.level = this.level;
        req.pageIndex = this.pageIndex;
        req.pageSize = pageSize;
        req.userId = this.userId;
        model.initFans(req, new HttpResultSubscriber<ShopKeepFansEntity>() {
            @Override
            public void onSuccess(ShopKeepFansEntity entity) {
                if(entity.list.size() < pageSize){
                    view.fansResult(entity, true);
                }else {
                    view.fansResult(entity, false);
                    pageIndex++;
                }
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void loadMore() {
        ShopKeepFansReq req = new ShopKeepFansReq();
        req.level = this.level;
        req.pageIndex = this.pageIndex;
        req.pageSize = pageSize;
        req.userId = this.userId;
        model.initFans(req, new HttpResultSubscriber<ShopKeepFansEntity>() {
            @Override
            public void onSuccess(ShopKeepFansEntity entity) {
                if(entity.list.size() < pageSize){
                    view.loadFansResult(entity, true);
                }else {
                    view.loadFansResult(entity, false);
                    pageIndex++;
                }
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void initFansSum(int leve, int userId) {
        FansSumReq req = new FansSumReq();
        req.relationLevel = String.valueOf(level);
        req.userId = userId;

        model.fansSummy(req, new HttpResultSubscriber<FansSumEntity>() {
            @Override
            public void onSuccess(FansSumEntity fansSumEntity) {
                view.fansSum(fansSumEntity);
            }

            @Override
            public void _onError(int status, String msg) {

            }
        });
    }


}
